package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.beans.LeaveVO;
import com.icss.daos.LeaveDao;

public class LeaveServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public LeaveServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        List<LeaveVO> list=new ArrayList<LeaveVO>();
        LeaveDao dao=null;
        String leave=request.getParameter("leave");
        System.out.println("程序在LeaveServlet在doget");
        if(leave!=null){
        	if(leave.equals("query")){
        		String sql="select * from leave";
        		dao=new LeaveDao();
        		list=dao.selectLeave(sql);
        		request.setAttribute("leaveVo", list);
        		String url="/page/office/attendance/leave_query.jsp";
 			    RequestDispatcher rd=request.getRequestDispatcher(url);
 			    rd.forward(request, response);
        	}
        	
        }

	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        List<LeaveVO> list=new ArrayList<LeaveVO>();
        LeaveDao dao=null;
        String leave=request.getParameter("leave");
        System.out.println("程序在LeaveServlet在doPost");
       
        if(leave!=null){
        	if(leave.equals("add")){
        		String leaveName=  request.getParameter("txtName");
        		String leaveReason= request.getParameter("txtLeaveType");    		
        		String shenpiPerson=request.getParameter("sltLeader");               
        		Date beginTime = null;  
        		Date endTime=null;
                if(request.getParameter("txtLeaveDate1")!=null&&request.getParameter("txtLeaveDate1")!=""){
                	beginTime=Date.valueOf(request.getParameter("txtLeaveDate1"));
                } 
                if(request.getParameter("txtLeaveDate2")!=null&&request.getParameter("txtLeaveDate2")!=""){
                	endTime=Date.valueOf(request.getParameter("txtLeaveDate2"));
                }
        		String sql="insert into leave(leaveName,leaveReason,beginTime,endTime,shenpiPerson)" +
        				"values('"+leaveName+"','"+leaveReason+"','"+beginTime+"','"+endTime+"','"+shenpiPerson+"')";
        		 dao=new LeaveDao();
        		dao.insertLeave(sql);
        	}
        }
	
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
